package com.hpin.fansea.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.Address;

import java.util.List;

/**
 * 收货地址的item Adapter
 * @author Aaron
 *
 */
public class AddressAdapter extends BaseAdapter {
	
	private List<Address> addressLists;
	private Context ctx;
	private String communityAddress;
	
	public AddressAdapter(Context ctx, List<Address> addressLists, String address) {
		super();
		this.addressLists = addressLists;
		this.ctx = ctx;
		this.communityAddress = address;
	}
	
	public void setAddressLists(List<Address> addressLists) {
		this.addressLists = addressLists;
	}

	@Override
	public int getCount() {
		return addressLists.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = View.inflate(ctx, R.layout.item_address, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.tv_tel = (TextView) convertView.findViewById(R.id.tv_tel);
			viewHolder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
			
			viewHolder.tv_default = (TextView) convertView.findViewById(R.id.tv_default);
			viewHolder.iv_detail = (ImageView) convertView.findViewById(R.id.iv_detail);
			
			viewHolder.iv_select = (ImageView) convertView.findViewById(R.id.iv_select);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		//防止复用造成的数据紊乱问题 
		viewHolder.tv_default.setVisibility(View.INVISIBLE);
		viewHolder.iv_detail.setVisibility(View.INVISIBLE);
		viewHolder.iv_select.setSelected(false);
		
		viewHolder.tv_name.setText(addressLists.get(position).receiver);
		viewHolder.tv_tel.setText(addressLists.get(position).contactWay);
		viewHolder.tv_detail.setText(communityAddress + addressLists.get(position).address);
		
		//如果是默认地址 则显示
		if (addressLists.get(position).isDefault == 1) {
			viewHolder.tv_default.setVisibility(View.VISIBLE);
			viewHolder.iv_detail.setVisibility(View.VISIBLE);
		}
		
		if (addressLists.get(position).isSelect) {
			viewHolder.iv_select.setSelected(true);
		}
		
		return convertView;
	}

	
	private class ViewHolder {
		/**收件人*/
		private TextView tv_name;
		/**收件人电话*/
		private TextView tv_tel;
		/**收件地址*/
		private TextView tv_detail;
		/**默认地址的蓝色字*/
		private TextView tv_default;
		/**默认地址的蓝色图标*/
		private ImageView iv_detail;
		/**默认地址的前面的红色图标*/
		private ImageView iv_select;

	}
	
	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
}
